package com.ruoyi.common.enums.wecom;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum WecomMsgActionEnum {
    SEND("send", "发送消息"),
    RECALL("recall", "撤回消息"),
    SWITCH("switch", "切换企业日志"),
    ;

    private final String code;
    private final String desc;

    private static final Map<String, WecomMsgActionEnum> ENUM_MAP;

    static {
        WecomMsgActionEnum[] values = values();
        Map<String, WecomMsgActionEnum> codeToEnum = new HashMap<>(values.length);
        for (WecomMsgActionEnum value : values) {
            codeToEnum.put(value.getCode(), value);
        }
        ENUM_MAP = Collections.unmodifiableMap(codeToEnum);
    }

    /**
     * 根据动作代码查找枚举
     */
    public static WecomMsgActionEnum getEnumByCode(String code) {
        WecomMsgActionEnum wecomMsgActionEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(wecomMsgActionEnum)) {
            throw new RuntimeException("消息动作类型有误: " + code);
        }
        return wecomMsgActionEnum;
    }

    /**
     * 检查动作代码是否存在
     */
    public static boolean contains(String code) {
        return ENUM_MAP.containsKey(code);
    }

    /**
     * 安全获取枚举，如果不存在返回null
     */
    public static WecomMsgActionEnum getEnumByCodeSafe(String code) {
        return ENUM_MAP.get(code);
    }
}
